package com.lwj.service.administration;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.lwj.constant.ConstantDefinition;
import com.lwj.dto.administration.UserDto;
import com.lwj.dto.administration.UserListDto;
import com.lwj.exception.BusinessException;
import com.lwj.mapper.administration.UserMapper;
import com.lwj.model.administration.Department;
import com.lwj.model.administration.User;
import com.lwj.service.DozerService;
import com.lwj.service.SpringUtil;
import com.lwj.service.configuration.UserRoleService;
import com.lwj.vo.PageBean;
import com.lwj.vo.administration.UserVo;
import com.lwj.vo.configuration.RoleInfo;
import com.lwj.vo.configuration.UserInfo;
import com.lwj.vo.configuration.UserPermissionVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.stream.Collectors;

@Service("userService")
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper userMapper;
    @Autowired
    private DozerService dozerService;
    @Autowired
    private DepartmentService departmentService;
    @Autowired
    private UserRoleService userRoleService;

    @Override
    public UserVo getUserById(UserDto dto) {
        return userMapper.selectUserVo(dto.getEmpId());
    }
    @Override
    public void insertUser(UserDto dto) {
        User user = dozerService.map(dto, User.class);
        String userId = SpringUtil.getUserId();
        user.create(userId);
        userMapper.insertSelective(user);
    }
    @Override
    public UserInfo login(String empId, String password) {
        HttpServletRequest request = SpringUtil.getRequest();
        User user = new User();
        user.setEmpId(empId);
        user.setPassword(password);
        UserVo userVo = userMapper.selectByNameAndPassword(user);
        if(userVo==null){
           throw new BusinessException("用户不存在");
        }
        UserInfo userInfo = userInfo(userVo);
        request.getSession().setAttribute(ConstantDefinition.SESSION_KEY_USERNAME,userInfo);
        return userInfo;
    }
    public UserInfo userInfo(UserVo userVo){
        String empId = userVo.getEmpId();
        String name = userVo.getName();
        String departmentRoleId="";
        List<RoleInfo> roleInfos = userMapper.selectRolesByEmpId(empId);
        if(!CollectionUtils.isEmpty(roleInfos)){
            departmentRoleId=roleInfos.get(0).getDepartmentRoleId();
        }
        List<UserPermissionVo> userPermissionVos = userRoleService.selectUserPermission(empId, null);
        List<String> codes = userPermissionVos.stream().filter(item -> !"0".equals(item.getCurrentLevel()) || !"0".equals(item.getNextLevel())).map(UserPermissionVo::getPermissionCode).distinct().collect(Collectors.toList());
        return new UserInfo(empId,name,userVo.getDepartmentId(),departmentRoleId,codes,roleInfos);
    }

    @Override
    public   PageBean<UserVo>  selectUserByDepartmentId(UserListDto dto) {
        if(StringUtils.hasText(dto.getDepartmentId())){
            Department department = departmentService.selectByPrimaryKey(dto.getDepartmentId());
            dto.setPath(department.getPath());
        }else {
            dto.setPath("1000");
        }
        Page<UserVo> objects = PageHelper.startPage(dto.getPageNumber(), dto.getPageSize());
        userMapper.selectUserByDepartmentId(dto);
        return PageBean.page(objects);
    }

    @Override
    public String save(UserDto dto) {
        User user = dozerService.map(dto, User.class);
        user.setEnableState(0);
        user.setPassword("888888");
        userMapper.insertSelective(user);
        return user.getEmpId();
    }

    @Override
    public int changeState(String empId,Integer enableState) {
        User user=new User();
        user.setEmpId(empId);
        user.setEnableState(enableState);
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public int delete(String empId) {
        User user=new User();
        user.setEmpId(empId);
        user.setDr("1");
        return userMapper.updateByPrimaryKeySelective(user);
    }

    @Override
    public  List<RoleInfo>  selectUserRoles() {
        String userId = SpringUtil.getUserId();
        return userMapper.selectRolesByEmpId(userId);
    }

    @Override
    public void out() {
        HttpSession session = SpringUtil.getSession();
        session.removeAttribute(ConstantDefinition.SESSION_KEY_USERNAME);
    }
}
